
#import "BlessingViewController.h"
#import "blessingCustomCell.h"
#import "ResponseManager.h"


static NSString *blessingIdentifier = @"blessingTableViewCell";
@interface BlessingViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataSoure;

@end

@implementation BlessingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupBackItem];
    self.title = @"福包动态";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults objectForKey:@"userInfo"];
    NSString *str = dic[@"id"];
    
    NSDictionary *parameters = @{@"user_id":str};
    
    [ResponseManager startGetRequestWithURLString:kBlessingURL parameters:parameters completionHandler:^(FBResponse *response) {
        
        if (response.success) {
            NSLog(@"福包%@",response.resultValue);
            self.dataSoure = response.resultValue;
            [self.tableView reloadData];
        }else {
            NSLog(@"失败 %@",response.resultDesc);
        }
        
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSoure.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    blessingCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:blessingIdentifier];
    NSString *str = self.dataSoure[indexPath.row][@"add_time"];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:[str integerValue]];
    NSString *timeString = [formatter stringFromDate:confromTimesp];
    cell.time.text = [NSString stringWithFormat:@"%@领到福包",timeString];
    cell.coin.text = self.dataSoure[indexPath.row][@"coin"];
    
    
    return cell;
    
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        UINib *nib = [UINib nibWithNibName:@"blessingCustomCell" bundle:nil];
        [_tableView registerNib:nib forCellReuseIdentifier:blessingIdentifier];
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

- (NSMutableArray *)dataSoure {
    if (!_dataSoure) {
        _dataSoure = [NSMutableArray array];
    }
    return _dataSoure;
}




@end
